package com.example.sortlistview01.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sortlistview01.activity.R;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.view.ViewPropertyAnimator;

public class SideBarLinerlayout extends LinearLayout {
	// 26个字母
	public static String[] b = { "A", "B", "C", "D", "E", "F", "G", "H", "I",
			"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
			"W", "X", "Y", "Z", "#" };
	private View[] views;

	private final int itemWidth = 30;
	private final int itemHeight = 24;
	private final int disCount = 1;
	private long expandDuration = 100;
	private final float distance = 100;
	private final int increase = 50;
	private VelocityTracker velocityTracker;

	public SideBarLinerlayout(Context context) {
		this(context, null);
	}

	public SideBarLinerlayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public SideBarLinerlayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		if (getHeight() != 0) {
			removeAllViews();
			for (int i = 0; i < b.length; i++) {
				LinearLayout.LayoutParams params = new LayoutParams(itemWidth,
						itemHeight);
				params.gravity = Gravity.CENTER;
				addView(views[i], params);
			}
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		int action = event.getAction();
		float y = event.getY();
		int index = (int) (y / getHeight() * b.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.

		if (index >= b.length) {
			index = b.length - 1;
		}
		if (index < 0) {
			index = 0;
		}
		

		switch (action) {
		case MotionEvent.ACTION_UP:
			touchDismiss();
			break;
		case MotionEvent.ACTION_DOWN:
			if(velocityTracker == null){
				velocityTracker = VelocityTracker.obtain();//获得VelocityTracker类实例 
			}else {
				velocityTracker.clear();
			}
			velocityTracker.addMovement(event);
			touchItem(index);
			break;
		case MotionEvent.ACTION_MOVE:
			velocityTracker.addMovement(event);
			velocityTracker.computeCurrentVelocity(1000);
			if(Math.abs(velocityTracker.getYVelocity()) <= 100){
				expandDuration = 200;
			}else if(Math.abs(velocityTracker.getYVelocity()) >= 200){
				expandDuration = 0;
			}else {
				expandDuration = 100;
			}
//			System.out.println("##############velocityTracker.getYVelocity() ===" + velocityTracker.getYVelocity());
			touchMoveItem(index);
			break;
		default:
			break;
		}
		return true;
	}

	@SuppressWarnings("deprecation")
	private void touchDismiss() {
		// setBackgroundDrawable(new ColorDrawable(0x00000000));
		for (int i = 0; i < b.length; i++) {
			expand(views[i], 0);
		}
	}

	private float x0;

	private void expandForMove(final int index, final View view,
			final float distance) {
		ViewPropertyAnimator.animate(view).translationX(distance)
				.setListener(new AnimatorListener() {
					@Override
					public void onAnimationStart(Animator animation) {

					}

					@Override
					public void onAnimationRepeat(Animator animation) {

					}

					@Override
					public void onAnimationEnd(Animator animation) {

					}

					@Override
					public void onAnimationCancel(Animator animation) {

					}
				}).setDuration(expandDuration);
	}

	private void expand(final View view, final float distance) {
		ViewPropertyAnimator.animate(view).translationX(distance)
				.setDuration(expandDuration);
	}

	private static int index2 = -1;
	private static int lastIndex = -1;

	private int getPre(int index) {
		int pre = index - 1;
		if (pre < 0) {
			pre = 0;
		}
		return pre;
	}

	private int getAfter(int index) {
		int after = index + 1;
		if (after + 1 > b.length) {
			after = b.length - 1;
		}
		return after;
	}

	private void touchMoveItem(int index) {
		index2 = index;
//		System.out.println("##############index2===" + index2);

		if (lastIndex != index) {
//			System.out.println("##############lastIndex===" + lastIndex);
//			System.out.println("##############index===" + index);

			for (int i = 0; i < b.length; i++) {
				if (Math.abs(index - i) > 1) {
					if (views[i].getX() != x0) {
						expand(views[i], 0);
					}
				}
			}

			if (index == lastIndex - 1) {
				expand(views[getPre(index)], -distance + increase);
				expand(views[index], -distance);
				expand(views[getAfter(index)], -distance + increase);
				expand(views[getAfter(index + 1)], 0);
			} else if (index == lastIndex + 1) {
				expand(views[getPre(index - 1)], 0);
				expand(views[getPre(index)], -distance + increase);
				expand(views[index], -distance);
				expand(views[getAfter(index)], -distance + increase);
			} else {
				expand(views[getPre(index)], -distance + increase);
				expand(views[index], -distance);
				expand(views[getAfter(index)], -distance + increase);
			}
			lastIndex = index;
		}
	}

	private void touchItem(int index) {
		index2 = index;
		lastIndex = index;
		setBackgroundResource(R.drawable.sidebar_background);
		for (int i = 0; i < b.length; i++) {

			if (i == index) {
				x0 = views[i].getX();
				expandForMove(i, views[i], -distance);
			} else {
				int minus = index - i;
				if (minus > 0 && minus <= disCount) { // 点击item上面动向
					expandForMove(i, views[i], -distance + minus * increase);
				} else if (minus >= -disCount && minus < 0) { // 点击item下面动向
					expandForMove(i, views[i], -distance - minus * increase);
				} else {

				}
			}

		}
	}

	private void initView() {
		setOrientation(LinearLayout.VERTICAL);
		views = new View[b.length];
		for (int i = 0; i < b.length; i++) {
			TextView child = new TextView(getContext());
			child.setText(b[i]);
			views[i] = child;
		}

	}

}
